package cn.pconline.autoclub.web.admin;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.gelivable.web.Env;
import org.gelivable.web.EnvUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.pconline.autoclub.entity.DailyGeneralSituation;
import cn.pconline.autoclub.service.DailyGeneralSituationService;
import cn.pconline.autoclub.util.Pager;
import java.util.Map;

/**
 *
 * @author zhoutianhuai
 */
@Controller
@RequestMapping("/admin")
public class DailyGeneralSituationController {
    @Autowired
    private DailyGeneralSituationService dailyGeneralSituationService;
    private static final Logger log = LoggerFactory.getLogger(DailyGeneralSituationController.class);
    
    /**
     * 车友会总体情况统计查询
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/dailyGeneralSituationList.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        //param
        String beginDateStr = env.param("beginDate", "");
        String endDateStr = env.param("endDate", "");
        int pageNo = env.paramInt("pageNum", 1);
        int pageSize = env.paramInt("pageSize", 20);
        
        int beginDate = 0;
        int endDate = 0;
        if(!beginDateStr.isEmpty()){
        	beginDate = Integer.parseInt(beginDateStr.replaceAll("-", ""));
        }
        if(!endDateStr.isEmpty()){
        	endDate = Integer.parseInt(endDateStr.replaceAll("-", ""));
        }
        Pager<DailyGeneralSituation> pager = dailyGeneralSituationService.getDailyGeneralSituationForPage(beginDate, endDate, pageNo, pageSize);
        
        Map<String, Object> map = dailyGeneralSituationService.getAvgNum(beginDate, endDate, pageNo, pageSize);
        //return
        req.setAttribute("pager", pager);    
        req.setAttribute("avg", map);    
        req.setAttribute("beginDate", beginDateStr);
        req.setAttribute("endDate", endDateStr);
        req.setAttribute("pageNo", pageNo);
        req.setAttribute("pageSize", pageSize);
        return "admin/club/dailyGeneralSituationList";
    }
    
    /**
     * 车友会总体情况统计查询导出
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/dailyGeneralSituationExport.do", produces = {"application/octet-stream"})
    public void dailyGeneralSituationExport(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        //param
        String beginDateStr = env.param("beginDate", "");
        String endDateStr = env.param("endDate", "");
        int pageNo = env.paramInt("pageNum", 1);
        int pageSize = env.paramInt("pageSize", 20);
        
        int beginDate = 0;
        int endDate = 0;
        if(!beginDateStr.isEmpty()){
        	beginDate = Integer.parseInt(beginDateStr.replaceAll("-", ""));
        }
        if(!endDateStr.isEmpty()){
        	endDate = Integer.parseInt(endDateStr.replaceAll("-", ""));
        }
        Pager<DailyGeneralSituation> pager = dailyGeneralSituationService.getDailyGeneralSituationForPage(beginDate, endDate, pageNo, pageSize);
        
        //导出excel
        String[] cellString = new String[]{"所属日期", "登录用户总数", "发表主题帖总数", "发表说说总数", "说说评论总数", "回帖总数", "点赞总数"};
        HSSFWorkbook work = new HSSFWorkbook();
        HSSFSheet sheet = work.createSheet("车友会总体情况列表");
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < cellString.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(cellString[i]);
        }
        /**
         * 根据分页数据生成excel文件
         */
        for (int curPage = pageNo; curPage <= pager.getPageCount(); curPage++) {
            // 数据一页一页加载
            if (curPage != pageNo) {
                pager = dailyGeneralSituationService.getDailyGeneralSituationForPage(beginDate, endDate, curPage, pageSize);
            }  
            for (int i = 0; i < pager.getResultList().size(); i++) {
            	DailyGeneralSituation d = pager.getResultList().get(i);
                HSSFRow rowData = sheet.createRow(((curPage - 1) * pageSize) + i + 1);
                rowData.createCell(0).setCellValue(d.getBelongsDate());
                rowData.createCell(1).setCellValue(d.getVisitorCount());
                rowData.createCell(2).setCellValue(d.getTopicCount());
                rowData.createCell(3).setCellValue(d.getCommentCount());
                rowData.createCell(4).setCellValue(d.getCommentReplyCount());
                rowData.createCell(5).setCellValue(d.getTopicReplyCount());
                rowData.createCell(6).setCellValue(d.getPraiseCount());
            }
        }

        try {
            //设置返回数据类型
            resp.setContentType("application/vnd.ms-excel;charset=GBK");
            //设置返回的文件名
            resp.setHeader("Content-Disposition", 
            		"attachment;Filename=ClubDailyGeneralSituation.xls");
            OutputStream out = resp.getOutputStream();
            work.write(out);
            out.flush();
            out.close();
        } catch (IOException ex) {
            log.error("ClubDailyGeneralSituation fail!", ex);
        }
    }
}